layui.use(['table', 'laydate', 'admin', 'ax', 'func', 'form'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;
    var laydate = layui.laydate;
    var form = layui.form;


    /**
     * 管理
     */
    var BtyGoodsPay = {
        tableId: "btyGoodsPayTable"
    };

    /**
     * 初始化财务人员列表
     */
    var cwryAjax = new $ax(Feng.ctxPath + "/btyGoodsPay/selectAllCwry");
    var cwryResult = cwryAjax.start();
    console.info(cwryResult);
    $.each(cwryResult, function (index, val) {
        $("#cwry").append("<option value='" + val.cwry + "'>" + val.cwry + "</option>");
    });
    form.render('select');

    //年月选择器
    laydate.render({
        elem: '#month'
        , type: 'month'
    });

    form.on('submit(btnSubmit)', function (data) {
        BtyGoodsPayinitDatas();
    });

    BtyGoodsPayinitDatas = function () {
        if ($('#month').val() == "") {
            Feng.error("请选择月份");
            return;
        }
        var ajax = new $ax(Feng.ctxPath + "/btyGoodsPay/initData", function (data) {
            if (data.code == 1) {
                Feng.error("初始化数据失败!" + data.message);
                return;
            }
            Feng.success("初始化数据成功！");
            table.reload(BtyGoodsPay.tableId);

        }, function (data) {
            Feng.error("添加失败！" + data.responseJSON.message)
        });
        ajax.set("month", $("#month").val());
        ajax.start();
        return false;
    };


    BtyGoodsPay.onSendItem = function (data) {

        var ajax = new $ax(Feng.ctxPath + "/btyGoodsPay/send", function (data) {
            if (data.success == true) {
                Feng.success("发送成功!");
                table.reload(BtyGoodsPay.tableId);
            } else {
                Feng.error(data.message);
            }


        }, function (data) {
            Feng.error("发送失败!" + data.responseJSON.message + "!");
        });
        ajax.set("id", data.id);
        ajax.start();

    };

    /**
     * 点击审核按钮
     * @param data
     */
    BtyGoodsPay.onAuditItem = function (data) {
        let index = layer.open({
            area: ['300px', '200px'],
            title: '审核',
            btn: ['通过', '驳回'],
            btnAlign: 'c',
            type: 1,
            async: false,
            content: '<div style="padding: 20px 20px;">是否确认通过审核？</div>',
            yes: function (index, layero) {
                //提交审核结果 通过状态为3：已通过
                BtyGoodsPay.shenHe(3, data.id);
                //table.reload(BtyGoodsPay.tableId);
                layer.close(index);
            },
            btn2: function (index, layero,) {

                //提交审核结果 驳回状态为4：已驳回
                BtyGoodsPay.shenHe(4, data.id);
                layer.close(index);
            },
        });

    };

    /**
     * 提交审核结果
     */
    BtyGoodsPay.shenHe = function (status, id,) {
        let obj = {};
        obj.id = id;
        obj.status = status;
        //obj.content = reason;

        $.ajax({
            type: "post",
            url: "/btyGoodsPay/audit",
            data: obj,
            async: false,
            success: function (rs) {
                if(rs.success){
                    if (status == 3) {
                        layer.msg('已通过！');
                    } else {
                        layer.msg('已驳回！');
                    }
                }else{
                    Feng.error(rs.message);
                }
                // parent.location.reload();
                table.reload(BtyGoodsPay.tableId);
            }
        });
    };

    // 修改还款状态点击事件
    $('#batchSend').click(function () {
        BtyGoodsPay.batchSend();
    });

    BtyGoodsPay.batchSend = function () {
        var checkRows = table.checkStatus(BtyGoodsPay.tableId);
        var data = checkRows.data;
        var ids = "";
        if (checkRows.data.length === 0) {
            Feng.error("请选择要批量发送的数据");
        } else {
            for (var i in data) {
                ids += data[i].id + ",";
            }
            var operation = function () {
                var ajax = new $ax(Feng.ctxPath + "/btyGoodsPay/batchSend", function (data) {
                    if(data.code=201){
                        Feng.success(data.message);
                    }else {
                        Feng.success("发送成功!");
                        table.reload(Notice.tableId);
                    }



                }, function (data) {
                    Feng.error("发送失败!" + data.responseJSON.message + "!");
                });
                ajax.set("ids", ids);
                ajax.start();
            };
            Feng.confirm("是否批量发送?", operation);
        }
    };





    /**
     * 初始化表格的列
     */
    BtyGoodsPay.initColumn = function () {
        return [[
            {type: 'checkbox',fixed:'left'},
            // {field: 'id', hide: true, title: ''},
            //{field: 'yaoma', sort: true, title: '妖码'},
            {field: 'dm', sort: false, title: '店名',fixed:'left',width:190},
            {field: 'cwm', sort: false, title: '编码',width:60,align: 'center'},
            {field: 'cwry', sort: false, title: '财务',width:60,align: 'center'},
            {field: 'shopownerView', title: '店长',width:60,align: 'center'},
            {field: 'mobile', title: '店长手机',align: 'center',width:110},
            // {field: 'mainPay', sort: false, title: '酱料酒水鱼平板货赔',width:120,align: 'center'},
            // {field: 'canJu', sort: false, title: '餐具货赔',width:70,align: 'center'},
            // {field: 'faKuan', sort: false, title: '罚款',width: 50,align: 'center'},
            // {field: 'missed', sort: false, title: '漏扣',width: 50,align: 'center'},
            {field: 'sumMoney', sort: false, title: '货赔总金额',width: 100,align: 'center'},
            {field: 'cwDeduction', sort: false, title: '财务扣款', width: 100, align: 'center'},
            {field: 'month', sort: false, title: '月份',width:60,align: 'center'},
            {field: 'dzName', title: '店长', event: 'setdz', width: 120},
            // {align: 'left', toolbar: '#tableBar', title: '操作',minWidth: '140',},
            // {field: 'name1', sort: false, title: '姓名',width:60,align: 'center'},
            // {field: 'money1', sort: false, title: '金额',width:50,align: 'center'},
            // {field: 'name2', sort: false, title: '姓名',width:60,align: 'center'},
            // {field: 'money2', sort: false, title: '金额',width:50,align: 'center'},
            // {field: 'name3', sort: false, title: '姓名',width:60,align: 'center'},
            // {field: 'money3', sort: false, title: '金额',width:50,align: 'center'},
            // {field: 'name4', sort: false, title: '姓名',width:60,align: 'center'},
            // {field: 'money4', sort: false, title: '金额',width:50,align: 'center'},
            // {field: 'name5', sort: false, title: '姓名',width:60,align: 'center'},
            // {field: 'money5', sort: false, title: '金额',width:50,align: 'center'},
            {field: 'isCompliance', title: '是否合规',templet:function (d) {
                if(d.isCompliance === 0) {
                    return '合规';
                } else if (d.isCompliance === 1) {
                    return '不合规';
                } else {
                    return '不合规';
                }
            }},
            {field: 'status', sort: false, title: '状态', templet: '#statusTpl',width:80,fixed: 'right',align: 'center'},
            {field: 'remark', sort: false, title: '备注',fixed: 'right',align: 'center'}
            // {field: 'createTime', sort: true, title: '创建时间'},
            // {field: 'createUser', sort: true, title: '创建人'},
            // {field: 'updateTime', sort: true, title: '更新时间'},
            // {field: 'updateUser', sort: true, title: '更新人'},

        ]];
    };

    /**
     * 点击查询按钮
     */
    var queryData = {};
    BtyGoodsPay.search = function () {
        queryData['dm'] = $('#dm').val();
        queryData['cwry'] = $('#cwry').val();
        queryData['month'] = $('#month').val();
        queryData['status'] = $('#status').val();
        queryData['isCompliance'] = $('#isCompliance').val();

        table.reload(BtyGoodsPay.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 弹出添加对话框
     */
    BtyGoodsPay.openAddDlg = function () {
        func.open({
            title: '添加',
            content: Feng.ctxPath + '/btyGoodsPay/add',
            tableId: BtyGoodsPay.tableId
        });
    };

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    BtyGoodsPay.openEditDlg = function (data) {
        func.open({
            title: '修改',
            content: Feng.ctxPath + '/btyGoodsPay/edit?id=' + data.id,
            tableId: BtyGoodsPay.tableId
        });
    };


    /**
     * 导出excel按钮
     */
    BtyGoodsPay.exportExcel = function () {
        var checkRows = table.checkStatus(BtyGoodsPay.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    BtyGoodsPay.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/btyGoodsPay/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(BtyGoodsPay.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    BtyGoodsPay.tableCfg = table.render({
        elem: '#' + BtyGoodsPay.tableId,
        url: Feng.ctxPath + '/btyGoodsPay/listAll',
        limit:20,
        page: true,
        height: "full-59",
        cellMinWidth: 100,
        where: {month: $("#month").val()},
        cols: BtyGoodsPay.initColumn()
    });

    //首次加载表格
    queryData['month'] = $('#month').val();
    BtyGoodsPay.tableCfg.where = queryData;
    table.render(BtyGoodsPay.tableCfg);

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        BtyGoodsPay.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {

        BtyGoodsPay.openAddDlg();

    });

    // 导出excel
    $('#btnExp').click(function () {
        BtyGoodsPay.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + BtyGoodsPay.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            BtyGoodsPay.openEditDlg(data);
        } else if (layEvent === 'delete') {
            BtyGoodsPay.onDeleteItem(data);
        } else if (layEvent === 'send') {
            BtyGoodsPay.onSendItem(data);
        } else if (layEvent === 'audit') {
            BtyGoodsPay.onAuditItem(data);
        }
    });

    // 按月导出
    $('#batchExport').click(function () {
        var month= $('#month').val();
        if(month==""||month==undefined||month==null){
            Feng.error("请选择月份");
        }else {
            //获取导出Excel的数据
            var url=Feng.ctxPath + "/btyGoodsPay/listAllExport?month="+month;
            location.href = url;
        }
    });
});
